﻿namespace REVISIONMAX20
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    public class frmSource : Form
    {
        private UIHandler _uiHandler = null;
        private ToolStripButton btnApply;
        private Button btnBrowse;
        private ToolStripButton btnSave;
        private CheckBox chkRecursive;
        private IContainer components = null;
        private FolderBrowserDialog folderBrowserDialog1;
        private Label label2;
        private Label label9;
        private Source source = null;
        private ToolStripSeparator toolStripSeparator3;
        private ToolStripSeparator toolStripSeparator4;
        private ToolStrip tsMain;
        private MyTextBox txtRegEx;
        private MyTextBox txtSource;

        public frmSource()
        {
            this.InitializeComponent();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            this.Save(true);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1.RootFolder = Environment.SpecialFolder.MyComputer;
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtSource.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Save(false);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.toolStripSeparator4 = new System.Windows.Forms.ToolStripSeparator();
            this.tsMain = new System.Windows.Forms.ToolStrip();
            this.btnSave = new System.Windows.Forms.ToolStripButton();
            this.btnApply = new System.Windows.Forms.ToolStripButton();
            this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
            this.label9 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.btnBrowse = new System.Windows.Forms.Button();
            this.txtSource = new REVISIONMAX20.MyTextBox(this.components);
            this.txtRegEx = new REVISIONMAX20.MyTextBox(this.components);
            this.chkRecursive = new System.Windows.Forms.CheckBox();
            this.tsMain.SuspendLayout();
            this.SuspendLayout();
            // 
            // toolStripSeparator4
            // 
            this.toolStripSeparator4.Name = "toolStripSeparator4";
            this.toolStripSeparator4.Size = new System.Drawing.Size(6, 25);
            // 
            // tsMain
            // 
            this.tsMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.btnSave,
            this.toolStripSeparator4,
            this.btnApply,
            this.toolStripSeparator3});
            this.tsMain.Location = new System.Drawing.Point(0, 0);
            this.tsMain.Name = "tsMain";
            this.tsMain.Size = new System.Drawing.Size(602, 25);
            this.tsMain.TabIndex = 31;
            this.tsMain.Text = "toolStrip1";
            // 
            // btnSave
            // 
            this.btnSave.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnSave.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(35, 22);
            this.btnSave.Text = "Save";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // btnApply
            // 
            this.btnApply.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Text;
            this.btnApply.ImageTransparentColor = System.Drawing.Color.Magenta;
            this.btnApply.Name = "btnApply";
            this.btnApply.Size = new System.Drawing.Size(38, 22);
            this.btnApply.Text = "Apply";
            this.btnApply.Click += new System.EventHandler(this.btnApply_Click);
            // 
            // toolStripSeparator3
            // 
            this.toolStripSeparator3.Name = "toolStripSeparator3";
            this.toolStripSeparator3.Size = new System.Drawing.Size(6, 25);
            // 
            // label9
            // 
            this.label9.Location = new System.Drawing.Point(4, 33);
            this.label9.Name = "label9";
            this.label9.Size = new System.Drawing.Size(80, 20);
            this.label9.TabIndex = 28;
            this.label9.Text = "Source";
            // 
            // label2
            // 
            this.label2.Location = new System.Drawing.Point(4, 86);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(80, 20);
            this.label2.TabIndex = 29;
            this.label2.Text = "regex";
            // 
            // btnBrowse
            // 
            this.btnBrowse.Location = new System.Drawing.Point(559, 33);
            this.btnBrowse.Name = "btnBrowse";
            this.btnBrowse.Size = new System.Drawing.Size(25, 20);
            this.btnBrowse.TabIndex = 32;
            this.btnBrowse.Text = "...";
            this.btnBrowse.UseVisualStyleBackColor = true;
            this.btnBrowse.Click += new System.EventHandler(this.btnBrowse_Click);
            // 
            // txtSource
            // 
            this.txtSource.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtSource.ForeColor = System.Drawing.Color.Blue;
            this.txtSource.Location = new System.Drawing.Point(90, 33);
            this.txtSource.Name = "txtSource";
            this.txtSource.Size = new System.Drawing.Size(466, 20);
            this.txtSource.TabIndex = 17;
            this.txtSource.TextChanged += new System.EventHandler(this.txtSource_TextChanged);
            // 
            // txtRegEx
            // 
            this.txtRegEx.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.txtRegEx.ForeColor = System.Drawing.Color.Blue;
            this.txtRegEx.Location = new System.Drawing.Point(90, 86);
            this.txtRegEx.Multiline = true;
            this.txtRegEx.Name = "txtRegEx";
            this.txtRegEx.Size = new System.Drawing.Size(466, 74);
            this.txtRegEx.TabIndex = 18;
            // 
            // chkRecursive
            // 
            this.chkRecursive.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.chkRecursive.Location = new System.Drawing.Point(0, 56);
            this.chkRecursive.Name = "chkRecursive";
            this.chkRecursive.Size = new System.Drawing.Size(105, 24);
            this.chkRecursive.TabIndex = 33;
            this.chkRecursive.Text = "Recursive";
            this.chkRecursive.UseVisualStyleBackColor = true;
            // 
            // frmSource
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(602, 234);
            this.Controls.Add(this.chkRecursive);
            this.Controls.Add(this.btnBrowse);
            this.Controls.Add(this.tsMain);
            this.Controls.Add(this.label9);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txtSource);
            this.Controls.Add(this.txtRegEx);
            this.Name = "frmSource";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
            this.tsMain.ResumeLayout(false);
            this.tsMain.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void Save(bool apply)
        {
            this.UpdateObject();
            StringBuilder builder = this.source.SaveOrUpdate();
            if (builder.Length > 0)
            {
                MessageBox.Show(builder.ToString());
            }
            else if (!apply)
            {
                base.Close();
            }
        }

        private void txtSource_TextChanged(object sender, EventArgs e)
        {
            this.Text = "Source: " + this.txtSource.Text;
        }

        private void UpdateObject()
        {
            this.source.source = this.txtSource.Text;
            this.source.regex = this.txtRegEx.Text;
            this.source.recursive = this.chkRecursive.Checked;
        }

        private void UpdateViewer()
        {
            this.txtSource.Text = this.source.source;
            this.txtRegEx.Text = this.source.regex;
            this.chkRecursive.Checked = this.source.recursive;
        }

        public void View(UIHandler puiHandler, Source pSource)
        {
            this._uiHandler = puiHandler;
            this.source = pSource;
            this.UpdateViewer();
            base.ShowDialog();
        }

        public int id
        {
            get
            {
                return this.source.id;
            }
        }
    }
}

